import 'dart:convert';
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/widges/appbar.dart';
import 'package:guochaoapp/controller/shopping/shopping_controller.dart';
import 'package:guochaoapp/controller/shopping/shopping_payment_controller.dart';
import 'package:guochaoapp/controller/shopping/shopping_settle_controller.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';
import 'package:guochaoapp/model/create_order_entity.dart';
import 'package:guochaoapp/model/freight_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/view/shopping/shopping_list_page.dart';
import '../../r.dart';

class GCShoppingPaymentPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    GCShoppingPaymentController c = Get.put(GCShoppingPaymentController());
    if (Get.arguments != null) {
      c.mallOrderNo = Get.arguments["mallOrderNo"];
      c.requestPayMentInit();
    }
    return Scaffold(
        appBar: GCAppBar(
            titleText: "支付",
            backTap: () {
              Get.until((route) => Get.currentRoute == Routes.INITIAL);
              GCShoppingController shopping_c = Get.find();
              shopping_c.requestInitData();
            }),
        body: Obx(() => _initSubviews()));
  }

  _initSubviews() {
    GCShoppingPaymentController c = Get.put(GCShoppingPaymentController());
    // GCShoppingSettleController shoppingSettleController = Get.find();

    List<Widget> items = [
      Padding(
        padding: const EdgeInsets.only(left: 15, right: 14, top: 21),
        child: Container(
          height: 64,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(11)),
              boxShadow: [
                BoxShadow(
                  offset: Offset(0, 1),
                  color: Color(0xFFE7ECEF),
                )
              ],
              color: Colors.white),
          child: Row(children: [
            SizedBox(width: 20),
            Text("支付金额",
                style: TextStyle(color: Color(0xFF333333), fontSize: 16)),
            Expanded(child: SizedBox()),
            RichText(
              text: TextSpan(children: <InlineSpan>[
                TextSpan(
                    text: "¥",
                    style: TextStyle(color: Color(0xFFFE674C), fontSize: 13)),
                TextSpan(
                    text: c.paymentinitData.value.paymentPrice.toString(),
                    style: TextStyle(color: Color(0xFFFE674C), fontSize: 18)),
              ]),
            ),
            SizedBox(width: 20),
          ]),
        ),
      ),
      SizedBox(height: 13),
      Row(
        children: [
          SizedBox(width: 22),
          Text("选择支付方式",
              style: TextStyle(color: Color(0xFF999999), fontSize: 14)),
        ],
      ),
    ];

    if (c.paymentinitData.value != null) {
      if (c.paymentinitData.value.openWx != null &&
          c.paymentinitData.value.openWx == true) {
        items.add(Padding(
          padding: const EdgeInsets.only(left: 15, right: 14, top: 11),
          child: GestureDetector(
              onTap: () {
                c.selectedIndex.value = 1;
              },
              child: Container(
                height: 64,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(11)),
                    boxShadow: [
                      BoxShadow(
                        offset: Offset(0, 1),
                        color: Color(0xFFE7ECEF),
                      )
                    ],
                    color: Colors.white),
                child: Row(children: [
                  SizedBox(width: 20),
                  Image.asset(R.assetsIconWxpay, width: 41, height: 41),
                  SizedBox(width: 17),
                  Text("微信支付",
                      style: TextStyle(color: Color(0xFF333333), fontSize: 16)),
                  Expanded(child: SizedBox()),
                  Image.asset(
                      c.selectedIndex == 1
                          ? R.assetsIconPaysel
                          : R.assetsIconPayunsel,
                      width: 18,
                      height: 18),
                  SizedBox(width: 20),
                ]),
              )),
        ));
      }

      if (c.paymentinitData.value.openAli != null &&
          c.paymentinitData.value.openAli == true) {
        items.add(Padding(
            padding: const EdgeInsets.only(left: 15, right: 14, top: 11),
            child: GestureDetector(
              onTap: () {
                c.selectedIndex.value = 2;
              },
              child: Container(
                height: 64,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(11)),
                    boxShadow: [
                      BoxShadow(
                        offset: Offset(0, 1),
                        color: Color(0xFFE7ECEF),
                      )
                    ],
                    color: Colors.white),
                child: Row(children: [
                  SizedBox(width: 20),
                  Image.asset(R.assetsIconAlipay, width: 41, height: 41),
                  SizedBox(width: 17),
                  Text("支付宝支付",
                      style: TextStyle(color: Color(0xFF333333), fontSize: 16)),
                  Expanded(child: SizedBox()),
                  Image.asset(
                      c.selectedIndex == 2
                          ? R.assetsIconPaysel
                          : R.assetsIconPayunsel,
                      width: 18,
                      height: 18),
                  SizedBox(width: 20),
                ]),
              ),
            )));
      }

      if (c.paymentinitData.value.openTransfer != null &&
          c.paymentinitData.value.openTransfer == true) {
        items.add(Padding(
            padding: const EdgeInsets.only(left: 15, right: 14, top: 11),
            child: GestureDetector(
              onTap: () {
                c.selectedIndex.value = 3;
              },
              child: Container(
                height: 64,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(11)),
                    boxShadow: [
                      BoxShadow(
                        offset: Offset(0, 1),
                        color: Color(0xFFE7ECEF),
                      )
                    ],
                    color: Colors.white),
                child: Row(children: [
                  SizedBox(width: 20),
                  Image.asset(R.assetsIconBankcardpay, width: 41, height: 41),
                  SizedBox(width: 17),
                  Text("对公转账",
                      style: TextStyle(color: Color(0xFF333333), fontSize: 16)),
                  Expanded(child: SizedBox()),
                  Image.asset(
                      c.selectedIndex == 3
                          ? R.assetsIconPaysel
                          : R.assetsIconPayunsel,
                      width: 18,
                      height: 18),
                  SizedBox(width: 20),
                ]),
              ),
            )));
      }

      if (c.paymentinitData.value.openCredit != null &&
          c.paymentinitData.value.openCredit == true) {
        items.add(Padding(
            padding: const EdgeInsets.only(left: 15, right: 14, top: 11),
            child: GestureDetector(
                onTap: () {
                  c.selectedIndex.value = 4;
                },
                child: Container(
                  height: 64,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(11)),
                      boxShadow: [
                        BoxShadow(
                          offset: Offset(0, 1),
                          color: Color(0xFFE7ECEF),
                        )
                      ],
                      color: Colors.white),
                  child: Row(children: [
                    SizedBox(width: 20),
                    Image.asset(R.assetsIconXypay, width: 41, height: 41),
                    SizedBox(width: 17),
                    Text("信用支付",
                        style:
                            TextStyle(color: Color(0xFF333333), fontSize: 16)),
                    SizedBox(width: 5),
                    Image.asset(R.assetsIconVippay, width: 20, height: 18),
                    Expanded(child: SizedBox()),
                    Image.asset(
                        c.selectedIndex == 4
                            ? R.assetsIconPaysel
                            : R.assetsIconPayunsel,
                        width: 18,
                        height: 18),
                    SizedBox(width: 20),
                  ]),
                ))));
      }
    }

    items.add(Expanded(
        child: Container(
      padding: const EdgeInsets.only(left: 15, right: 14, top: 11, bottom: 20),
      child: Container(
          child: Column(children: [
        Expanded(child: SizedBox()),
        RichText(
          text: TextSpan(children: <InlineSpan>[
            TextSpan(
                text: "请于",
                style: TextStyle(color: Color(0x77000000), fontSize: 14)),
            TextSpan(
                text: c.getDisplayTime(),
                style: TextStyle(
                    color: Color(0xFFFE674C),
                    fontSize: 17,
                    fontWeight: FontWeight.bold)),
            TextSpan(
                text: "内支付完成",
                style: TextStyle(color: Color(0x77000000), fontSize: 14)),
            TextSpan(
                text: "超时订单需重新提交",
                style: TextStyle(color: Color(0xFFFE674C), fontSize: 14)),
          ]),
        ),
        SizedBox(height: 5),
        Container(
          width: double.infinity,
          height: 50,
          child: FlatButton(
              color: Theme.of(Get.context).primaryColor,
              textColor: Colors.white,
              disabledColor: Colors.white,
              disabledTextColor: Colors.black,
              onPressed: c.selectedIndex.value != 0
                  ? () {
                      c.submitTap();
                    }
                  : null,
              child: Text("确认", style: TextStyle(fontSize: 16))),
        )
      ])),
    )));

    return Column(children: items);
  }
}
